package utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {

	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub
		 HelloLucene h = new HelloLucene();  
	      List<Product> plist = new ArrayList<Product>();  
	  
	      Product p = new Product();  
	      p.setId("1");  
	      p.setContent(" 我是朋友我你说呢哇纷纷反而反而个");  
	      Product p1 = new Product();  
	      p1.setId("2");  
	      p1.setContent("中华人民我们是");  
	      Product p2 = new Product();  
	      p2.setId("3");  
	      p2.setContent("圣地阿森纳屌丝男士");  
	      Product p3 = new Product();  
	      p3.setId("4");  
	      p3.setContent("收定金多少奶粉丰富让我给你温柔跟那人那个人 ");  
	        
	      plist.add(p);  
	      plist.add(p1);  
	      plist.add(p2);  
	      plist.add(p3);  
	        
	      h.helloL("", plist);  
	}
	 public String helloL(String indexPath,List<Product> productList) throws IOException, ParseException{  
	        Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_4_10_3);  
	         // Store the index in memory:  
	        Directory directory = new RAMDirectory();  
	        // To store an index on disk, use this instead:  
	        //Directory directory = FSDirectory.open(new File("H:/luceneIndex"));  
	        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);  
	        IndexWriter iwriter = new IndexWriter(directory, config);  
	        for(Product product:productList){  
	            Document doc = new Document();  
	            doc.add(new Field("id",product.getId()+"",TextField.TYPE_STORED));  
	            doc.add(new Field("content",product.getContent()+"",TextField.TYPE_STORED));  
	            iwriter.addDocument(doc);  
	        }  
	        iwriter.close();  
	        System.out.println("建立索引 ok");  
	          
	          
	        DirectoryReader ireader = DirectoryReader.open(directory);  
	        IndexSearcher isearcher = new IndexSearcher(ireader);  
	        // Parse a simple query that searches for "text":  
	        QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "content", analyzer);  
	        Query query = parser.parse("我们");  
	        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;  
	        System.out.println(hits.length == 1);  
	        // Iterate through the results:  
	        for (int i = 0; i < hits.length; i++) {  
	          Document hitDoc = isearcher.doc(hits[i].doc);  
	          System.out.println(hitDoc.get("content"));  
	          System.out.println(hitDoc.get("id"));  
	  
	        }  
	        ireader.close();  
	        directory.close();  
	          
	        return "";  
	    }  
}
